<?php

namespace App\Models;


/**
 * App\Models\Role
 *
 * @property int $id
 * @property string $name 角色名称
 * @property string $describe 描述
 * @property int $status 状态1正常2锁定
 * @property array $access_ids 权限ID
 * @method static \Illuminate\Database\Eloquent\Builder|Role newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Role newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Role query()
 * @method static \Illuminate\Database\Eloquent\Builder|Role whereAccessIds($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Role whereDescribe($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Role whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Role whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Role whereStatus($value)
 * @mixin \Eloquent
 */
class Role extends Base{

    protected $table = 'roles';

    const STATUS_1 = 1;
    const STATUS_2 = 2;

    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];

    protected $fillable = [
        'id','name','describe','access_ids','status'
    ];

    public $timestamps = false;//关闭自动维护

    protected $casts = ['access_ids' => 'array'];


    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*']){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('name','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['status']) && $params['status']) {
                    $query ->where('status',$params['status']);
                }
            })->orderBy('id', 'desc')->paginate(config('app.limit'));
        $this->stateToText($lists,[
            'status' => self::STATUS_TEXT,
        ]);
        return $lists;
    }

    protected function roles(){
        $roles = $this->select(['id','name'])->where('status',self::STATUS_1)->get()->toArray();
        return $roles;
    }
}
